package retrospection.组合总和;

import java.util.ArrayList;
import java.util.List;

public class Solution {
    public List<List<Integer>> combinationSum(int[] candidates, int target) {
        ArrayList<List<Integer>> lists = new ArrayList<>();
        retrospection(candidates,target,0,0,lists,new ArrayList<>());
        return lists;

    }

    public void retrospection(int[] candidates, int target,int index , int current, List<List<Integer>> ans, List<Integer> temp) {
        if (current >= target) {
            if (current == target) {
                ans.add(new ArrayList<>(temp));
            }
            return;
        }
        for (int i = index; i < candidates.length; i++) {
            temp.add(candidates[i]);
            current += candidates[i];
            retrospection(candidates,target,i,current,ans,temp);
            temp.remove(temp.size()-1);
            current-= candidates[i];
        }
    }

    public static void main(String[] args) {
        System.out.println(new Solution().combinationSum(new int[]{2,3,6,7},7));
    }
}
